package com.ywjh.inter.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils
{
	private static DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
	private static DateFormat dfInfo=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static DateFormat dfInfo2=new SimpleDateFormat("yyyy-MM-dd HH");
	
	public static String getDateStrYYYYMMddHH(Timestamp timestamp){
		return dfInfo2.format(new Date(timestamp.getTime()));
	}
	
	public static Date parseDateYYYYMMddHH(String str){
		try {
			return dfInfo2.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}
	
	public static Date parseDateYYYYMMdd(String str){
		try {
			return df.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}
	
	/**
	 * 获取制定日期的全格式 yyyy-MM-dd HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String getDateFormatInfo(Date date)
	{
		String code="";
		if(date!=null)
		{
			code=dfInfo.format(date);
		}
		return code;
	}
	
	public static String getStartTime(){
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.DATE, 1);
		return df.format(cal.getTime());
	}
	
	public static String getEndTime(){
		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.MONTH, 1);
		cal.set(Calendar.DATE,1);
		cal.add(Calendar.DATE,-1);
		return df.format(cal.getTime());
	}
	
	/**
	 * 获取制定日期的年月日格式 yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String getDateFormat(Date date)
	{
		String code="";
		if(date!=null)
		{
			code=df.format(date);
		}
		return code;
	}
	
	/**
	 * 获取当前时间的前一天日期
	 * @return
	 */
	public static Date getPreDay()
	{
		Calendar calendar=Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		return calendar.getTime();
	}
	
	/**
	 * 获取指定时间的前一天日期
	 * @param date
	 * @return
	 */
	public static Date getPreDay(Date date)
	{
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);
		return calendar.getTime();
	}
	
	/**
	 * 获取指定时间的后一天日期
	 * @param date
	 * @return
	 */
	public static Date getNextDay(Date date)
	{
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, +1);
		return calendar.getTime();
	}
	
	public static Date parseDate(String time)
	{
		Date date=null;
		try {
			date=df.parse(time);
		} catch (Exception e) {
		}
		return date;
	}
	
	public static Date parseDateInfo(String time)
	{
		Date date=null;
		try {
			date=dfInfo.parse(time);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}
}
